package cron

import (
	"cdam/enum"
	"cdam/models"
	"cdam/qiongqi"
	"github.com/astaxie/beego/toolbox"
	"log"
)

type Scheduleable struct {
	count    int
	Name     string
}

// 启动单个流程的任务
func (c *Scheduleable) LoadProcess(code string, name string) error {
	nodes,err := models.GetEventByProcessCode(code)
	if err == nil {
		for _,node := range nodes {
			if node.Type != enum.EVENTTYPE_STARTTIMEEVENT.String() {
				continue
			}
			toolbox.DeleteTask(node.Code)
			task := toolbox.NewTask(node.Code,node.TriggerExpression, func() error {
				parameterProcess := qiongqi.QiongqiProcessParam{}
				parameterProcess.InstanceName = name
				parameterProcess.ProcessCode = code
				parameterProcess.CurrentTaskHandler = "2"
				process := qiongqi.NewQiongqiProcess(parameterProcess)
				process.StartWorkflowInstance()
				return nil
			})
			toolbox.AddTask(node.Code ,task)
		}
	}
	return nil
}

// 启动所有流程
func (c *Scheduleable) LoadAllProcess()  {

}

func (c *Scheduleable) LoadTask(name string, code string, expression string)  {
	toolbox.DeleteTask(name)
	task := toolbox.NewTask(name,expression, func() error {
		parameterProcess := qiongqi.QiongqiProcessParam{}
		parameterProcess.InstanceName = name
		parameterProcess.ProcessCode = code
		parameterProcess.CurrentTaskHandler = "2"
		process := qiongqi.NewQiongqiProcess(parameterProcess)
		process.StartWorkflowInstance()
		return nil
	})
	toolbox.AddTask(name,task)
}

func (c *Scheduleable) LoadAllTask() error {
	return nil
}

func (c *Scheduleable) StartAllTask()  {
	log.Print("the start task count is " , c.count)
	toolbox.StartTask()
}

func Init() Scheduleable  {
	return Scheduleable{}
}
